package demo1.Layouts;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class MyLayout extends LayoutAdapter {
    List<Component> list = new ArrayList<>();

    @Override
    public void addLayoutComponent(Component comp, Object constraints) {
        list.add(comp);
    }

    @Override
    public void removeLayoutComponent(Component comp) {
        list.remove(comp);
    }

    @Override
    public void layoutContainer(Container parent) {
        System.out.println("提示：已重新布局");

        int width = parent.getWidth();
        int height = parent.getHeight();
        //Component[] children = parent.getComponents();
        int x = 0;
        int y = 0;
        for (int i = 0; i < list.size(); i++) {
            Component component = list.get(i);
            component.setBounds(x,y,100,100);
            x += 100;
            if(x > width) {
                x = 0;
                y += 100;
            }
        }
    }
}
